package com.yuke.cloud.service.pmc.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.pmc.dto.ProdCategoryClassDto;
import com.yuke.cloud.service.pmc.entity.ProdClass;
import com.yuke.cloud.service.pmc.service.ProdClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by zn on 2020/11/26.
*/
@Api(value = "WEB - ProdClassController", tags = "处理ProdClass相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/prod/class")
public class ProdClassController extends BaseController {
    @Resource
    private ProdClassService prodClassService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "prodClass", value = "该数据记录的POJO") @RequestBody ProdClass prodClass) {
        if (prodClass == null || prodClass.getCategoryId() == null || prodClass.getClassName() == null){
            return WrapMapper.error("参数错误！");
        }

        ProdClass cl = new ProdClass();
        cl.setCategoryId(prodClass.getCategoryId());
        cl.setClassName(prodClass.getClassName());
        Integer num = prodClassService.selectCount(cl);

        if (num > 0){
            return WrapMapper.error("存在相同名称记录！");
        }

        prodClass.setClassId(generateId());
        prodClassService.save(prodClass);
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        prodClassService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "prodClass", value = "需要修改的内容") @RequestBody ProdClass prodClass) {
        prodClassService.update(prodClass);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<ProdClass> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        ProdClass prodClass = prodClassService.findById(id);
        return WrapMapper.ok(prodClass);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<ProdClass>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<ProdClass> list = prodClassService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    /**
     * 通过分类id获取所有已绑定的类目Class
     */

    @GetMapping("/categoryId/bind/{categoryId}")
    @ApiOperation(httpMethod = "GET", value = "通过分类id获取所有已绑定的属性")
    public Wrapper<PageInfo<ProdCategoryClassDto>> getBindClassByCategoryId(@ApiParam(name = "categoryId", value = "记录id") @PathVariable Long categoryId,
                                                                            @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                                            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<ProdCategoryClassDto> prodCategoryClassDtos = prodClassService.getBindClassByCategoryId(categoryId);
        PageInfo pageInfo = new PageInfo(prodCategoryClassDtos);
        return WrapMapper.ok(pageInfo);
    }

    @PostMapping("/batchDelete")
    @ApiOperation(httpMethod = "POST", value = "根据id删除多条记录")
    public Wrapper deleteByIds(@ApiParam(name = "classIds", value = "classId") @RequestParam String classIds) {

            prodClassService.deleteByIds(classIds);

        return WrapMapper.ok();
    }
}
